package com.yaoming.umengshare.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yaoming.umengshare.R;
import com.yaoming.umengshare.SharePlatform;
import com.yaoming.umengshare.UmShareConfig;

import java.util.List;

/**
 * Created by zwj on 2015/12/25.
 */
public class SharePanelAdapter extends BaseAdapter{
    private Context mContext;
    private int displayWidth;
    private List<SharePlatform> shareList;
    public  SharePanelAdapter(Context context, List<SharePlatform> list){
        this.mContext=context;
        this.shareList=list;
//        int addSize=4-list.size()%4;
//        for (int i = 0; i <addSize ; i++) {
//            list.add(new SharePlatform());
//        }
        WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        displayWidth= wm.getDefaultDisplay().getWidth();

    }
    @Override
    public int getCount() {
        return shareList.size();
    }

    @Override
    public Object getItem(int position) {
        return shareList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            holder=new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.share_panel_item, null);
            holder.platform_icon=(ImageView)convertView.findViewById(R.id.platform_icon);
            holder.platform_name=(TextView)convertView.findViewById(R.id.platform_name);

            if (UmShareConfig.isChangeTextColor()){
                holder.platform_name.setTextColor(UmShareConfig.getTextColor());
            }
            if (UmShareConfig.isChangeTextSize()){
                holder.platform_name.setTextSize(UmShareConfig.getTextSize());
            }
            int size;
            if (UmShareConfig.isChangeNumColumns()){
                if (UmShareConfig.isChangeImageScale()){
                    size=(int)(displayWidth/UmShareConfig.getNumColumns()*UmShareConfig.getImageScale());
                }else {
                    size=(int)(displayWidth/UmShareConfig.getNumColumns()*0.6);
                }
            }else {
                if (UmShareConfig.isChangeImageScale()){
                    size=(int)(displayWidth/4*UmShareConfig.getImageScale());
                }else {
                    size=(int)(displayWidth/4*0.6);
                }
            }
            holder.platform_icon.setLayoutParams(new LinearLayout.LayoutParams(size,size));
            convertView.setTag(holder);
        }else {
            holder=(ViewHolder)convertView.getTag();
        }
        if (shareList.get(position).getMedia()!=null){
            holder.platform_icon.setImageResource(shareList.get(position).getIcon());
            holder.platform_name.setText(shareList.get(position).getName());
        }


        return convertView;
    }
    public class ViewHolder{
        private ImageView platform_icon;
        private TextView platform_name;
    }
}
